/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testing_class;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author minaev
 */
public class EmulStend extends Thread {

    DatagramSocket ds;
    DatagramPacket dp;
    
    int order_by_transm=0;
    int cur_mot = 0;
    int selector_utka = 0;
    int mot_en[] = new int[8];
    int mot_emstop[] = new int[8];
    long mot_pos[] = new long[256];
    long mot_pos_trg[] = new long[256];
    int mot_i = 255;
    int tableMotorOn = 1;
    byte cur_id = 0xf;
    int id_udp = 1;
    int arp_who_is_131[] = {
        0xf4, 0x55, 0x9c, 0x66, 0x3f, 0x41, 0x38, 0x60, 0x77, 0x04, 0x7b, 0x8a, 0x08, 0x06, 0x00, 0x01,
        0x08, 0x00, 0x06, 0x04, 0x00, 0x02, 0x38, 0x60, 0x77, 0x04, 0x7b, 0x8a, 0xc0, 0xa8, 0x00, 0x83,
        0xf4, 0x55, 0x9c, 0x66, 0x3f, 0x41, 0xc0, 0xa8, 0x00, 0x8b
    };
    //src IP: 192.168.0.132
    int data_frame[] = {
        /*0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0xaa, 0xbb, 0xcc, 0x08, 0x00, 0x45, 0x00,
         0x00, 0x32, 0x38, 0xd3, 0x00, 0x00, 0x80, 0x11, 0x7f, 0x12, 0xc0, 0xa8, 0x00, 0x84, 0xc0, 0xa8,
         0x00, 0xff, 0xc9, 0x39, 0xc9, 0x3a, 0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00*/
        0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0xaa, 0xbb, 0xcc, 0x08, 0x00, 0x45, 0x00,
        0x00, 0x27, 0x01, 0x5c, 0x00, 0x00, 0x80, 0x11, 0xb6, 0x97, 0xc0, 0xa8, 0x00, 0x84, 0xc0, 0xa8,
        0x00, 0xff, 0xc9, 0x39, 0xc9, 0x3a, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00
    };

    void inc_id_udp() {
        id_udp++;
        data_frame[0x12] = id_udp >> 8;
        data_frame[0x13] = id_udp & 0xFF;

        data_frame[0x2A] |= (0xF & id_udp);

        //data_frame[0x2D] = id_udp & 0xFF;


    }
    int och1 = 0;
    long ch1 = 0;
    int ch2 = 0;
    int ic = 0;

    void update_checksums() {
        ch1 = 0;
        for (ic = 0; ic < 10; ic += 2) {
            ch1 += (data_frame[ic + 0x0E] << 8) | data_frame[ic + 1 + 0x0E];

            if (ch1 > 0xFFFF) {
                ch1 -= 0xFFFF;
            }
        }

        for (ic = 0; ic < 8; ic += 2) {
            ch1 += (data_frame[0x1A + ic] << 8) | data_frame[0x1A + ic + 1];

            if (ch1 > 0xFFFF) {
                ch1 -= 0xFFFF;
            }
        }

        //ch1 = (ch1 & 0xff) + (ch1>>16);
        ch1 = ~ch1;

        data_frame[0x18] = (int) ((ch1 >> 8) & 0xFF);
        data_frame[0x19] = (int) (ch1 & 0xFF);
    }
    byte sbuf[] = new byte[10];
    byte rbuf[] = new byte[6];

    public EmulStend() throws SocketException {
        ds = new DatagramSocket(0xC93A);
        dp = new DatagramPacket(sbuf, 10);
    }
    Random r = new Random();
    byte cur_send_buf = 0;
    byte last_send_buf = 0;
    byte cnt_send_buf = 3;
    int send_buf[][] = new int[3][5];

    boolean valid_packet() {

        /*return ((Rx_Buff[0][0x1A]==192) &&
         (Rx_Buff[0][0x1B]==168) &&
         (Rx_Buff[0][0x1C]==0) &&
         (Rx_Buff[0][0x1D]==131 || Rx_Buff[0][0x1D]==133) &&
         //port
         (Rx_Buff[0][0x24]==0xc9) &&
         (Rx_Buff[0][0x25]==0x39)
         )?true:false;*/
        return true;
    }
    Runnable eth_receive = new Runnable() {
        @Override
        public void run() {

/*
            if ((0x0f & rbuf[0x0]) != cur_id) {
                if (valid_packet()) {


                    switch (Rx_Buff[0][0x2A] >> 4) {
                        //Motion
                        case 0x1: {
                            if ((Rx_Buff[0][0x2B] >> 7)) {
                                mot_pos_trg[cur_mot] += Rx_Buff[0][0x2B] & 0x7f;
                            } else {
                                mot_pos_trg[cur_mot] -= Rx_Buff[0][0x2B] & 0x7f;
                            }
                            setMotDis(cur_mot);
                            break;
                        }
                        //SetCurDvg
                        case 0x2: {
                            if (getMotEn(Rx_Buff[0][0x2B] ^ 1)) {
                                cur_mot = Rx_Buff[0][0x2B];
                                selector_utka = selector_utka & (~(1 << (cur_mot >> 1)))
                                        | ((cur_mot & 1) << (cur_mot >> 1));
                                tableMotorOn = 0;
                                //set rele selection
                                GPIOE - > ODR = GPIOE - > ODR & 0x3fff | (selector_utka << 14);
                                //delay(10000);
                                tableMotorOn = 1;

                            } else {
                                //ERROR, FRIEND DVIG WORKING
                                __NOP();
                            }
                            break;
                        }
                        //SetSpeed
                        case 0x3: {
                            //TIM6->PSC = 0x1C0 | (Rx_Buff[0][0x2B] & 0xC);
                            TIM6 - > PSC = 0x1C0 + (1024 - (Rx_Buff[0][0x2B] << 2));
                            break;
                        }
                        //GoToHome
                        case 0x4: {
                            mot_pos_trg[cur_mot] = 0;
                            setMotDis(cur_mot);
                            break;
                        }
                        //Em.Stop
                        case 0x5: {
                            mot_emstop[num >> 5] |= (1 << (Rx_Buff[0][0x2B] & 0x1f));
                            break;
                        }
                        //A.Em.Stop
                        case 0x6: {
                            mot_emstop[num >> 5] &= ~(1 << (Rx_Buff[0][0x2B] & 0x1f));
                            break;
                        }
                        //setTarget
                        case 0x7: {
                            //GPIOC->ODR ^= GPIO_Pin_9;
                            mot_pos_trg[cur_mot] = ((Rx_Buff[0][0x2C] << 24)
                                    | (Rx_Buff[0][0x2D] << 16)
                                    | (Rx_Buff[0][0x2E] << 8)
                                    | (Rx_Buff[0][0x2F]));

                            setMotDis(cur_mot);
                            //led_on();
                            break;
                        }

                        //setResearch
                        case 0x8: {
                            research = Rx_Buff[0][0x2B];
                            break;
                        }

                        //set step_count_ADC
                        case 0x9: {
                            steps_cnt_ADC = Rx_Buff[0][0x2B];
                            break;
                        }
                        default: {
                            __NOP();
                        }
                    }


                    cur_id = 0x0f & Rx_Buff[0][0x2A];
                }
            }*/
        }
    };

    @Override
    public void run() {



        while (true) {

            if ((last_send_buf != cur_send_buf) || order_by_transm!=0) {


                try {
                    Thread.sleep(10);
                } catch (InterruptedException ex) {
                    Logger.getLogger(EmulStend.class.getName()).log(Level.SEVERE, null, ex);
                }

                data_frame[0x2A] = 0x10;

                data_frame[0x31] = (int) (mot_pos[cur_mot] >> 24);
                data_frame[0x32] = (int) ((mot_pos[cur_mot] >> 16) & 0xff);
                data_frame[0x33] = (int) ((mot_pos[cur_mot] >> 8) & 0xff);
                data_frame[0x34] = (int) (mot_pos[cur_mot] & 0xff);


                //while (!(ADC1 - > SR & ADC_SR_JEOC));


                data_frame[0x2B] = r.nextInt(0x10);
                data_frame[0x2C] = r.nextInt(0x100);
                data_frame[0x2D] = r.nextInt(0x10);
                data_frame[0x2E] = r.nextInt(0x100);
                data_frame[0x2F] = r.nextInt(0x10);
                data_frame[0x30] = r.nextInt(0x100);

                inc_id_udp();
                update_checksums();

                for (int i = 0; i <= 10; i++) {
                    sbuf[i] = (byte) data_frame[0x2A + i];
                }

                //ds.send(dp);

                //tmp = ETH_HandleTxPkt( & data_frame, 53);
                last_send_buf = cur_send_buf;

                order_by_transm = 0;
                //delay(10);

            }

        }



        
    }
}
